package com.example.administrator.foodrowfresh.classfile;

import com.example.administrator.foodrowfresh.models.ClassifySeek;
import com.example.administrator.foodrowfresh.jiao.modle.PromotionContentModel;
import com.example.administrator.foodrowfresh.models.HomepageBody;
import com.example.administrator.foodrowfresh.models.TimeLimit;
import com.example.administrator.foodrowfresh.models.TopViewpage;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/4.
 */
public class JsonParserHomePage {
    /*
     * 首页轮播图解析json
     * */
    public static List<TopViewpage> parserJsonTopViewpage(String result) {
        try {
            JSONObject object = new JSONObject(result);
            JSONArray array = object.getJSONArray("data");
            List<TopViewpage> list = new ArrayList<>();
            for (int i = 0; i < array.length(); i++) {
                TopViewpage topViewpage = new TopViewpage();
                JSONObject jsonObject = array.getJSONObject(i);
                topViewpage.setType(jsonObject.getString("type"));
                topViewpage.setId(jsonObject.getString("id"));
                topViewpage.setUrl(jsonObject.getString("url"));
                topViewpage.setImage(jsonObject.getString("image"));
                list.add(topViewpage);
            }
            return list;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    /*
    * 限时抢购json解析
    * */
    public static List<TimeLimit.DataBean.FlashSaleBean> parserJsonTimeLimit(String result) {
        Gson gson = new Gson();
        TimeLimit timeLimit = gson.fromJson(result, TimeLimit.class);
        TimeLimit.DataBean dataBean = timeLimit.getData();
        List<TimeLimit.DataBean.FlashSaleBean> FlashSale = dataBean.getFlashSale();
        return FlashSale;
    }

    /*
    * 首页内容主体json解析
    * */
    public static List<HomepageBody> parserJsonHomepageBody(String result) {
        List<HomepageBody> floorItemBeans = new ArrayList<>();
        try {
            JSONObject jsonObject = new JSONObject(result);
            JSONArray jsonArray = jsonObject.getJSONArray("data");
            for (int i = 0; i < jsonArray.length(); i++) {
                HomepageBody homepageBody = new HomepageBody();
                JSONObject object = jsonArray.getJSONObject(i);

                homepageBody.setId(object.getString("Id"));
                homepageBody.setType(object.getString("Type"));
                homepageBody.setTitle(object.getString("Title"));
                homepageBody.setDescription(object.getString("Description"));
                homepageBody.setDisplayOrder(object.getString("DisplayOrder"));
                homepageBody.setBeginTime(object.getString("BeginTime"));
                homepageBody.setEndTime(object.getString("EndTime"));

                List<HomepageBody.FloorItemBean> list = new ArrayList<>();
                JSONArray array = object.getJSONArray("FloorItem");
                for (int j = 0; j < array.length(); j++) {
                    JSONObject jsonObj = array.getJSONObject(j);
                    HomepageBody.FloorItemBean floorItemBean = new HomepageBody.FloorItemBean();

                    floorItemBean.setProductId(jsonObj.optString("ProductId"));
                    floorItemBean.setProductName(jsonObj.optString("ProductName"));
                    floorItemBean.setWeight(jsonObj.optString("Weight"));
                    floorItemBean.setPRICE(jsonObj.optString("PRICE"));
                    floorItemBean.setOLDPRICE(jsonObj.optString("OLDPRICE"));
                    floorItemBean.setPRICENAME(jsonObj.optString("PRICENAME"));
                    floorItemBean.setPictureId(jsonObj.optString("PictureId"));
                    floorItemBean.setEndTime(jsonObj.optString("EndTime"));
                    floorItemBean.setImage(jsonObj.optString("image"));

                    floorItemBean.setId(jsonObj.optString("Id"));
                    floorItemBean.setFloorMasterId(jsonObj.optString("FloorMasterId"));
                    floorItemBean.setUrl(jsonObj.optString("Url"));
                    floorItemBean.setLinkType(jsonObj.optString("LinkType"));
                    floorItemBean.setLinkContent(jsonObj.optString("LinkContent"));
                    floorItemBean.setLinkContentName(jsonObj.optString("LinkContentName"));

                    list.add(floorItemBean);
                }
                homepageBody.setFloorItem(list);
                floorItemBeans.add(homepageBody);
            }
            return floorItemBeans;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
    /*
* jiao 详情页数据json解析
* PromotionContentModel.DataBean.ProductBean
* */
    public static PromotionContentModel.DataBean.ProductBean parserPromotion(String result) {
        Gson gson = new Gson();
        PromotionContentModel.DataBean dataBean = gson.fromJson(result, PromotionContentModel.class).getData();
        PromotionContentModel.DataBean.ProductBean productBeans = dataBean.getProduct();
        return productBeans;
    }

    /*
   * jiao 详情页数据json解析
   * PromotionContentModel.DataBean.ProductBean
   * */
    public static List<PromotionContentModel.DataBean.PromotionalActivitiesBean> promotionalActivitiesPromotion(String result) {
        Gson gson = new Gson();
        PromotionContentModel.DataBean dataBean = gson.fromJson(result, PromotionContentModel.class).getData();
        List<PromotionContentModel.DataBean.PromotionalActivitiesBean> promotionalActivities = dataBean.getPromotionalActivities();
        return promotionalActivities;
    }
    public static List<PromotionContentModel.DataBean.ProductserivesBean> namePromotion(String result) {
        Gson gson = new Gson();
        PromotionContentModel.DataBean dataBean = gson.fromJson(result, PromotionContentModel.class).getData();
        List<PromotionContentModel.DataBean.ProductserivesBean> productserives = dataBean.getProductserives();
        return productserives;
    }
    /*
    * 搜索分类
    * */
    public static List<ClassifySeek.DataBean> parserJsonSeek(String result)
    {
        Gson gson=new Gson();
        List<ClassifySeek.DataBean> list=gson.fromJson(result, ClassifySeek.class).getData();
        return list;
    }
}
